<?php
/**
 * 
 * 
 * 
 *
 */
final class Pagination {
	private $total = 0;
	private $page = 1;
	private $limit = 10;
	private $num_links = 10;
	private $url = '';
	private $text = '当前显示 {start} 至 {end}项, 共{total}项 ({pages} 页)';
	private $text_first = '第一页';
	private $text_last = '最后一页';
	private $text_next = '&raquo;';
	private $text_prev = '&laquo;';
	private $style_links = 'links';
	private $style_results = 'results';
	
	public function __construct($_total, $_url, $_page, $_limit = 10){
		$this->total = $_total;
		$this->url = $_url;
		$this->page = $_page;
		$this->limit = $_limit;
	}
	
	private function removePageparam($url){
		if($pos = strpos($url,'page')){
			return substr($url, 0, $pos-1);
		}
		return $url;
	}
	 
	public function render() {
		
		$this->url = $this->removePageparam($this->url);
		$pagParam = '&page={page}';
		if(false === strpos($this->url, '?')){
			$pagParam = '?page={page}';
		}
		$this->url .= $pagParam;
		$total = $this->total;
		
		if ($this->page < 1) {
			$page = 1;
		} else {
			$page = $this->page;
		}
		
		if (!$this->limit) {
			$limit = 10;
		} else {
			$limit = $this->limit;
		}
		
		$num_links = $this->num_links;
		$num_pages = ceil($total / $limit);
		
		$output = '';
		
		if ($page > 1) {
			$output .= '<li><a href="' . str_replace('{page}', 1, $this->url) . '">' . $this->text_first . '</a></li><li><a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a></li>';
    	}

		if ($num_pages >= 1) {
			if ($num_pages <= $num_links) {
				$start = 1;
				$end = $num_pages;
			} else {
				$start = $page - floor($num_links / 2);
				$end = $page + floor($num_links / 2);
			
				if ($start < 1) {
					$end += abs($start) + 1;
					$start = 1;
				}
						
				if ($end > $num_pages) {
					$start -= ($end - $num_pages);
					$end = $num_pages;
				}
			}

			if ($start > 1) {
				$output .= '<li class="disabled"><a>...</a></li>';
			}

			for ($i = $start; $i <= $end; $i++) {
				if ($page == $i) {
					$output .= '<li class="active"><a>' . $i . '</a></li>';
				} else {
					$output .= '<li><a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a></li>';
				}	
			}
							
			if ($end < $num_pages) {
				$output .= '<li class="disabled"><a>...</a></li>';
			}
		}
		
   		if ($page < $num_pages) {
			$output .= '<li><a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a> <a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>';
		}
		
		$find = array(
			'{start}',
			'{end}',
			'{total}',
			'{pages}'
		);
		
		$replace = array(
			($total) ? (($page - 1) * $limit) + 1 : 0,
			((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit),
			$total, 
			$num_pages
		);
		
		return ($output ? 
					'<ul class="' . $this->style_links . '">' . $output . '<li class="disabled ' . $this->style_results . '"><a>' . str_replace($find, $replace, $this->text) . '</a></li>' . '</ul>' 
					: 
					''
				);
	}
}
?>